//
//  ShopAddReduceOpertaionView.swift
//  sss
//
//  Created by Mac on 2021/1/13.
//  Copyright © 2021 hrrMac. All rights reserved.
//

import UIKit
// 增加减少商品的操作
class ShopAddReduceOpertaionView: UIView {
    /// 呈现视图的容器
    let containerView = UIView()
    /// 增加
    let addBtn = UIButton()
    /// 减少
    let reduceBtn = UIButton()
    /// 辅助视图
    private let layouGuide1 = UILayoutGuide()
    /// 数量控件
    let numberTextFiled = UITextField()
    /// 左边的框
    let reduceView = UIView()
    /// 左边的框
    let addView = UIView()
    /// 左边的线
    let leftLine = UIView()
    /// 右边的线
    let rightLine = UIView()
    
    
    /// 数量
    var number = 0
    /// 设置最大库存数，默认99
    var maxNumber = 99
    
    /// 错误事件的回调  1.有可能是超出了库存 2.有可能是已经是0 了还在减少
    var errorBlock: ((String)->Void)?
    /// 购物车数量修改 , 【Int】修改后的数量 【Int 】增加1 还是减少-1 修改数量0
    var shopNumberChangeBlock: ((Int,Int)->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    private func configUI(){
        configContainer()
        
        addBtn.addTarget(self, action: #selector(touchAddBtn), for: .touchUpInside)
        reduceBtn.addTarget(self, action: #selector(touchReduceBtn), for: .touchUpInside)
        
        addSubview(addBtn)
        addSubview(reduceBtn)
        addLayoutGuide(layouGuide1)
        
        refreshFrame()

    }
    
    private func configContainer(){
        containerView.layer.borderWidth = 0.5
        containerView.layer.borderColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1).cgColor
        containerView.layer.cornerRadius = 3
        
        addSubview(containerView)
        
        leftLine.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        rightLine.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)

        
        numberTextFiled.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        numberTextFiled.font = UIFont.systemFont(ofSize: 12)
        numberTextFiled.textAlignment = .center
        numberTextFiled.text = "0"
        numberTextFiled.keyboardType = .numberPad
        numberTextFiled.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
        numberTextFiled.delegate = self
        
        containerView.addSubview(reduceView)
        containerView.addSubview(addView)
        containerView.addSubview(leftLine)
        containerView.addSubview(rightLine)
        containerView.addSubview(numberTextFiled)
        
        drawReduceAddView()
        
    }
    
    /// 增加和减少的视图
    private func drawReduceAddView(){
        let re = UIView()
        re.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        reduceView.addSubview(re)
        
        re.translatesAutoresizingMaskIntoConstraints = false
        re.centerYAnchor.constraint(equalTo: reduceView.centerYAnchor).isActive = true
        re.centerXAnchor.constraint(equalTo: reduceView.centerXAnchor).isActive = true
        re.widthAnchor.constraint(equalToConstant: 12).isActive = true
        re.heightAnchor.constraint(equalToConstant: 2).isActive = true
        
        let h = UIView()
        h.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        let v = UIView()
        v.backgroundColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        addView.addSubview(h)
        addView.addSubview(v)
        
        h.translatesAutoresizingMaskIntoConstraints = false
        h.centerYAnchor.constraint(equalTo: addView.centerYAnchor).isActive = true
        h.centerXAnchor.constraint(equalTo: addView.centerXAnchor).isActive = true
        h.widthAnchor.constraint(equalToConstant: 12).isActive = true
        h.heightAnchor.constraint(equalToConstant: 2).isActive = true
        
        v.translatesAutoresizingMaskIntoConstraints = false
        v.centerYAnchor.constraint(equalTo: addView.centerYAnchor).isActive = true
        v.centerXAnchor.constraint(equalTo: addView.centerXAnchor).isActive = true
        v.widthAnchor.constraint(equalToConstant: 2).isActive = true
        v.heightAnchor.constraint(equalToConstant: 12).isActive = true
    }
    
    
    // 刷新视图的布局
    private func refreshFrame() {
        
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        containerView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        containerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        
        reduceView.translatesAutoresizingMaskIntoConstraints = false
        reduceView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        reduceView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        reduceView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
        reduceView.rightAnchor.constraint(equalTo: numberTextFiled.leftAnchor).isActive = true
        
        numberTextFiled.translatesAutoresizingMaskIntoConstraints = false
        numberTextFiled.leftAnchor.constraint(equalTo: reduceView.rightAnchor).isActive = true
        numberTextFiled.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        numberTextFiled.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
        numberTextFiled.rightAnchor.constraint(equalTo: addView.leftAnchor).isActive = true
        
        addView.translatesAutoresizingMaskIntoConstraints = false
        addView.leftAnchor.constraint(equalTo: numberTextFiled.rightAnchor).isActive = true
        addView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        addView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
        addView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        
        reduceView.widthAnchor.constraint(equalTo: addView.widthAnchor, multiplier: 1.0).isActive = true
        addView.widthAnchor.constraint(equalTo: numberTextFiled.widthAnchor, multiplier: 1.0).isActive = true
        
        leftLine.translatesAutoresizingMaskIntoConstraints = false
        leftLine.leftAnchor.constraint(equalTo: reduceView.rightAnchor).isActive = true
        leftLine.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        leftLine.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
        leftLine.widthAnchor.constraint(equalToConstant: 0.5).isActive = true

        rightLine.translatesAutoresizingMaskIntoConstraints = false
        rightLine.rightAnchor.constraint(equalTo: addView.leftAnchor,constant: -0.5).isActive = true
        rightLine.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        rightLine.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
        rightLine.widthAnchor.constraint(equalToConstant: 0.5).isActive = true
        
        reduceBtn.translatesAutoresizingMaskIntoConstraints = false
        reduceBtn.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        reduceBtn.rightAnchor.constraint(equalTo: layouGuide1.leftAnchor).isActive = true
        reduceBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        reduceBtn.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        
        layouGuide1.leftAnchor.constraint(equalTo: reduceBtn.rightAnchor).isActive = true
        layouGuide1.rightAnchor.constraint(equalTo: addBtn.leftAnchor).isActive = true
        layouGuide1.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        layouGuide1.heightAnchor.constraint(equalToConstant: 0.5).isActive = true
        
        addBtn.translatesAutoresizingMaskIntoConstraints = false
        addBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        addBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        addBtn.leftAnchor.constraint(equalTo: layouGuide1.rightAnchor,constant: 0).isActive = true
        addBtn.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
        
        reduceBtn.widthAnchor.constraint(equalTo: layouGuide1.widthAnchor, multiplier: 1.0).isActive = true
        layouGuide1.widthAnchor.constraint(equalTo: addBtn.widthAnchor, multiplier: 1.0).isActive = true
        
    }
    
    
    override func layoutSubviews() {
        refreshFrame()
    }
    
    

}

extension ShopAddReduceOpertaionView{
    func setParamert(_ num: Int){
        numberTextFiled.text = "\(num)"
    }
    
    
    /// 点击增加
    @objc private func touchAddBtn() {
        endEditing(true)
        if number >= maxNumber {
            if errorBlock != nil {
                errorBlock!("最多是能加入\(maxNumber)哦！")
            }
            return
        }
        number += 1
        if shopNumberChangeBlock != nil {
            shopNumberChangeBlock!(number,1)
        }
        setParamert(number)
    }
    /// 点击库存减少
    @objc private func touchReduceBtn() {
        endEditing(true)
        if number <= 0 {
            if errorBlock != nil {
                errorBlock!("已经不能再少了哦！")
            }
            return
        }
        number -= 1
        if shopNumberChangeBlock != nil {
            shopNumberChangeBlock!(number,-1)
        }
        setParamert(number)
    }
    
    @objc private func textFieldDidChange(textField: UITextField){
        #if DEBUG
        print("修改的商品参数\(textField.text ?? "0")")
        #endif
        if let num = Int(textField.text!) {
            if num > maxNumber {
                numberTextFiled.text = "\(number)"
            }else{
                number = num
            }
            
        }
    }
}

extension ShopAddReduceOpertaionView: UITextFieldDelegate{
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        #if DEBUG
        print("✅修改的商品参数\(textField.text ?? "0")")
        #endif
        if shopNumberChangeBlock != nil {
            shopNumberChangeBlock!(number,0)
        }
    }
}
